package com.supplychain.scm.core.feign.config;

import cn.hutool.core.map.MapUtil;
import com.supplychain.scm.core.feign.annotation.EnableDbySupplyFeign;
import com.supplychain.scm.core.feign.annotation.EnableGrayFeign;
import com.supplychain.scm.core.feign.gray.GrayLoadBalancerAutoConfiguration;
import com.google.common.collect.Maps;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Map;
import java.util.Optional;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName GrayLoadBalanceImporeSelect
 * @createTime 09:29:00 2023/02/13
 */
public class GrayLoadBalanceImportSelector implements ImportSelector {

    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        Map<String, Object> annotationAttributes = Optional.ofNullable(importingClassMetadata.getAnnotationAttributes(EnableDbySupplyFeign.class.getName())).orElse(Maps.newHashMap());
        if(MapUtil.isEmpty(annotationAttributes)) {
            annotationAttributes = Optional.ofNullable(importingClassMetadata.getAnnotationAttributes(EnableGrayFeign.class.getName())).orElse(annotationAttributes);
        }
        Boolean enableGray = Optional.ofNullable(annotationAttributes.get("enableGray")).map(gray -> ((Boolean) gray)).orElse(Boolean.FALSE);
        if(enableGray) {
            return new String[]{GrayLoadBalancerAutoConfiguration.class.getName()};
        }
        return new String[0];
    }
}
